package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.outside.train.query.SupplierOutsideOrderTrainDTO;
import com.ctshk.rpc.booking.dto.outside.train.query.SupplierOutsideOrderTrainTouristDTO;
import com.ctshk.rpc.booking.dto.outside.train.query.SupplierOutsideOrderTrainTouristTypeDTO;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderTrain;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderTrainTourist;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderTrainTouristType;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderTrainMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderTrainTouristMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderTrainTouristTypeMapper;
import com.ctshk.rpc.booking.req.outside.train.save.SupplierOutsideOrderTrainReq;
import com.ctshk.rpc.booking.service.ISupplierOutsideOrderTrainService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 站外火车票出行信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierOutsideOrderTrainServiceImpl implements ISupplierOutsideOrderTrainService {
    @Autowired
    SupplierOutsideOrderTrainMapper supplierOutsideOrderTrainMapper;
    @Autowired
    SupplierOutsideOrderTrainTouristMapper supplierOutsideOrderTrainTouristMapper;
    @Autowired
    SupplierOutsideOrderTrainTouristTypeMapper supplierOutsideOrderTrainTouristTypeMapper;

    @Override
    public void save(Long supplierOrderId, Long userId, LocalDateTime nowDateTime, List<SupplierOutsideOrderTrainReq> supplierOutsideOrderTrainReqList) {
        if (supplierOrderId != null) {
            //删除
            QueryWrapper<SupplierOutsideOrderTrain> outsideOrderTrainWrapper = new QueryWrapper<>();
            outsideOrderTrainWrapper.lambda().eq(SupplierOutsideOrderTrain::getOrderId, supplierOrderId);
            supplierOutsideOrderTrainMapper.delete(outsideOrderTrainWrapper);
            QueryWrapper<SupplierOutsideOrderTrainTouristType> outsideOrderTrainTouristTypeWrapper = new QueryWrapper<>();
            outsideOrderTrainTouristTypeWrapper.lambda().eq(SupplierOutsideOrderTrainTouristType::getOrderId, supplierOrderId);
            supplierOutsideOrderTrainTouristTypeMapper.delete(outsideOrderTrainTouristTypeWrapper);
            QueryWrapper<SupplierOutsideOrderTrainTourist> outsideOrderTrainTouristWrapper = new QueryWrapper<>();
            outsideOrderTrainTouristWrapper.lambda().eq(SupplierOutsideOrderTrainTourist::getOrderId, supplierOrderId);
            supplierOutsideOrderTrainTouristMapper.delete(outsideOrderTrainTouristWrapper);
        }
        //新增飞机信息
        List<SupplierOutsideOrderTrain> supplierOutsideOrderTrains = new ArrayList<>();
        List<SupplierOutsideOrderTrainTouristType> supplierOutsideOrderTrainTouristTypes = new ArrayList<>();
        List<SupplierOutsideOrderTrainTourist> supplierOutsideOrderTrainTourists = new ArrayList<>();
        supplierOutsideOrderTrainReqList.forEach(outsideOrderTrainReq -> {
            SupplierOutsideOrderTrain supplierOutsideOrderTrain = EntityUtil.copy(outsideOrderTrainReq, SupplierOutsideOrderTrain.class);
            supplierOutsideOrderTrain.setId(SnowflakeIdWorker.nextId());
            supplierOutsideOrderTrain.setOrderId(supplierOrderId);
            supplierOutsideOrderTrain.setCreateId(userId);
            supplierOutsideOrderTrain.setGmtCreate(nowDateTime);
            supplierOutsideOrderTrain.setModifiedId(userId);
            supplierOutsideOrderTrain.setGmtModified(nowDateTime);
            supplierOutsideOrderTrain.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierOutsideOrderTrains.add(supplierOutsideOrderTrain);
            if (CollectionUtils.isNotEmpty(outsideOrderTrainReq.getSupplierOutsideOrderTrainTouristTypeReqList())) {
                //新增旅客类型信息
                outsideOrderTrainReq.getSupplierOutsideOrderTrainTouristTypeReqList().forEach(outsideOrderTrainTouristTypeReq -> {
                    SupplierOutsideOrderTrainTouristType supplierOutsideOrderTrainTouristType = EntityUtil.copy(outsideOrderTrainTouristTypeReq, SupplierOutsideOrderTrainTouristType.class);
                    supplierOutsideOrderTrainTouristType.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderTrainTouristType.setOrderId(supplierOrderId);
                    supplierOutsideOrderTrainTouristType.setTrainId(supplierOutsideOrderTrain.getId());
                    supplierOutsideOrderTrainTouristType.setCreateId(userId);
                    supplierOutsideOrderTrainTouristType.setGmtCreate(nowDateTime);
                    supplierOutsideOrderTrainTouristType.setModifiedId(userId);
                    supplierOutsideOrderTrainTouristType.setGmtModified(nowDateTime);
                    supplierOutsideOrderTrainTouristType.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderTrainTouristTypes.add(supplierOutsideOrderTrainTouristType);
                    if (CollectionUtils.isNotEmpty(outsideOrderTrainTouristTypeReq.getSupplierOutsideOrderTrainTouristReqList())) {
                        outsideOrderTrainTouristTypeReq.getSupplierOutsideOrderTrainTouristReqList().forEach(outsideOrderTrainTouristReq -> {
                            SupplierOutsideOrderTrainTourist supplierOutsideOrderTrainTourist = EntityUtil.copy(outsideOrderTrainTouristReq, SupplierOutsideOrderTrainTourist.class);
                            supplierOutsideOrderTrainTourist.setId(SnowflakeIdWorker.nextId());
                            supplierOutsideOrderTrainTourist.setOrderId(supplierOrderId);
                            supplierOutsideOrderTrainTourist.setTrainId(supplierOutsideOrderTrain.getId());
                            supplierOutsideOrderTrainTourist.setCreateId(userId);
                            supplierOutsideOrderTrainTourist.setGmtCreate(nowDateTime);
                            supplierOutsideOrderTrainTourist.setModifiedId(userId);
                            supplierOutsideOrderTrainTourist.setGmtModified(nowDateTime);
                            supplierOutsideOrderTrainTourist.setIsDeleted(IsDeletedCode.NO.getCode());
                            supplierOutsideOrderTrainTourists.add(supplierOutsideOrderTrainTourist);

                        });
                    }
                });
            }
        });
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderTrains)) {
            supplierOutsideOrderTrainMapper.insertBatchSomeColumn(supplierOutsideOrderTrains);
        }
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderTrainTouristTypes)) {
            supplierOutsideOrderTrainTouristTypeMapper.insertBatchSomeColumn(supplierOutsideOrderTrainTouristTypes);
        }
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderTrainTourists)) {
            supplierOutsideOrderTrainTouristMapper.insertBatchSomeColumn(supplierOutsideOrderTrainTourists);
        }
    }

    @Override
    public List<SupplierOutsideOrderTrainDTO> selectByOrderId(Long id) {
        List<SupplierOutsideOrderTrainDTO> supplierOutsideOrderTrainDTOS = new ArrayList<>();
        //查询机票列表
        QueryWrapper<SupplierOutsideOrderTrain> trainWrapper = new QueryWrapper();
        trainWrapper.lambda().eq(SupplierOutsideOrderTrain::getOrderId, id);
        List<SupplierOutsideOrderTrain> supplierOutsideOrderTrains = supplierOutsideOrderTrainMapper.selectList(trainWrapper);
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderTrains)) {
            supplierOutsideOrderTrainDTOS = EntityUtil.copyList(supplierOutsideOrderTrains, SupplierOutsideOrderTrainDTO.class);
            supplierOutsideOrderTrainDTOS.forEach(train -> {
                //查询机票旅客类型列表
                QueryWrapper<SupplierOutsideOrderTrainTouristType> trainTouristTypeWrapper = new QueryWrapper();
                trainTouristTypeWrapper.lambda().eq(SupplierOutsideOrderTrainTouristType::getOrderId, id);
                List<SupplierOutsideOrderTrainTouristType> supplierOutsideOrderTrainTouristTypes = supplierOutsideOrderTrainTouristTypeMapper.selectList(trainTouristTypeWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderTrainTouristTypes)) {
                    List<SupplierOutsideOrderTrainTouristTypeDTO> supplierOutsideOrderTrainTouristTypeDTOList = EntityUtil.copyList(supplierOutsideOrderTrainTouristTypes, SupplierOutsideOrderTrainTouristTypeDTO.class);
                    train.setSupplierOutsideOrderTrainTouristTypeDTOList(supplierOutsideOrderTrainTouristTypeDTOList);
                    supplierOutsideOrderTrainTouristTypeDTOList.forEach(trainTouristType -> {
                        //查询机票旅客列表
                        QueryWrapper<SupplierOutsideOrderTrainTourist> trainTouristWrapper = new QueryWrapper();
                        trainTouristWrapper.lambda().eq(SupplierOutsideOrderTrainTourist::getOrderId, id);
                        List<SupplierOutsideOrderTrainTourist> supplierOutsideOrderTrainTourists = supplierOutsideOrderTrainTouristMapper.selectList(trainTouristWrapper);
                        if (CollectionUtils.isNotEmpty(supplierOutsideOrderTrainTourists)) {
                            List<SupplierOutsideOrderTrainTouristDTO> supplierOutsideOrderTrainTouristDTOList = EntityUtil.copyList(supplierOutsideOrderTrainTourists, SupplierOutsideOrderTrainTouristDTO.class);
                            trainTouristType.setSupplierOutsideOrderTrainTouristDTOList(supplierOutsideOrderTrainTouristDTOList);
                        }
                    });
                }
            });
        }
        return supplierOutsideOrderTrainDTOS;
    }
}
